package com.company.zoj;

import java.util.Scanner;

/**
 * Created by v on 17-6-5.
 */
public class ZojTest2451 {
    private static int[] vector;

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        try {
            while (true){
                int n=input.nextInt();
                int m=input.nextInt();

                vector=new int[n];
                for(int i=0;i<n;i++){
                    vector[i]=Integer.MAX_VALUE/2;
                }
                vector[0]=0;
                for(int i=0;i<m;i++){
                    int a=input.nextInt()-1;
                    int b=input.nextInt()-1;

                    int number=vector[a]+1;

                    while (vector[b]>number){
                        vector[b]=number;
                        b--;
                    }
                }

                System.out.println(vector[n-1]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
